package com.sjm.bookshop.email;

import com.sjm.bookshop.dao.LoginDao;
import com.sjm.bookshop.entity.Admin;
import com.sjm.bookshop.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class EmailService {
    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private LoginDao loginDao;

    public boolean verifyEmail(String phone, String email) throws MessagingException {
//        用户不为空则判断验证码次数
        User user = loginDao.checkUserEmail(phone,email);

//        设置key名
        String countEmailKey = "VerifyEmail:" + email + ":count";//邮箱key
        ValueOperations ops = redisTemplate.opsForValue();
        Object o1 = ops.get(countEmailKey);
        boolean flag;
        if (!user.equals(null)) {
//            验证countEmailKey是否存在
            if (redisTemplate.hasKey(countEmailKey) == false) {
//                设置过期时间为一天
                ops.set(countEmailKey, 1);
                redisTemplate.expire(countEmailKey,24*60*60*1000 , TimeUnit.MILLISECONDS);
                sendEmail(email);//发送邮件
                flag = true;
            } else if (Integer.parseInt(o1.toString())<=2) {
                ops.increment(countEmailKey);//次数加一
                sendEmail(email);//发送邮件
                flag = true;
            } else {
                flag = false;
            }
        } else {
            flag = false;
        }

        return flag;
    }

    /**
     *发送验证码邮件
     */

    public  void sendEmail(String email) throws MessagingException {
        ValueOperations ops = redisTemplate.opsForValue();
        String codeKey = "VerifyCode:" + email + ":code";//验证码key
        String code = getCode();
        ops.set(codeKey,code,2,TimeUnit.MINUTES);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//        构建有邮件对象
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//        设置邮件主题
        helper.setSubject("验证码");
//        设置邮件的发送者
        helper.setFrom("2451375713@qq.com");
//        设置邮件的收件人
        helper.setTo(email);
//        设置邮件正文内容
        helper.setText("<h2 style=\"text-align:center\">请在两分钟内输入验证码</h2>" +
                "<p style=\"text-align:center\">尊敬的用户,您好!您的验证码是"+
                "<span style=\"font-size:20px;color:red;\">"+code+"</span>"+"请在收到此邮件后的2分钟内进行验证</p>");
        javaMailSender.send(mimeMessage);
    }

    /**
     * 生成6位随机验证码
     *
     * @return
     */
    public static String getCode() {
        String code = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            code += (random.nextInt(9));
        }
        return code;
    }

    public int updateAdminPwd(String phone, String password) {
        return loginDao.resetAdminPwd(phone,password);
    }
}
